package net.aaa.hotel.dao.impl;

import net.aaa.hotel.dao.ITorderDao;
import net.aaa.hotel.entity.PageInfo;
import net.aaa.hotel.entity.TOrder;
import net.aaa.hotel.util.BaseDB;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class ITorderDaoImpl implements ITorderDao {
    /**
     * 根据预定的入住时间和离开时间  获取这中间的日期
     *
     * @param order
     * @return
     */
    @Override
    public void getDate(TOrder order) {
        String sql = "{call create_calendar('" + order.getRzdate() + "','" + order.getLeavedate() + "')}";
        BaseDB.getCall(sql);
    }

    @Override
    public List<Map> getAllInfo() {
        String sql = "select o.*, count(r.orderid) a \n" +
                "from t_order o left join t_ruzhudetail r \n" +
                "on o.id=r.orderid " +
                " group by o.id";
        return BaseDB.queryMap(sql);
    }

    @Override
    public List<Map> getPageInfo(PageInfo pageInfo) {
        String sql = "SELECT o.*, count(r.orderid) a , count(t.orderid) b " +
                " FROM t_order o LEFT JOIN t_ruzhudetail r ON o.id = r.orderid " +
                " LEFT JOIN t_tuifang t on t.ruzhuid = r.id "+
                " where 1=1 ";

        if (pageInfo.getCondition() != null) {
            sql += pageInfo.getCondition();
        }
        sql += " GROUP BY o.id limit ?,?";
        int start = (pageInfo.getCurr() - 1) * pageInfo.getNums();
        return BaseDB.queryMap(sql, start, pageInfo.getNums());
    }

    @Override
    public int getCount(String condition) {
        String sql = "SELECT count(*) aa \n" +
                "FROM ( SELECT o.*, count(r.orderid) a FROM t_order o LEFT JOIN t_ruzhudetail r ON o.id = r.orderid  where 1=1 ";
        if (condition != null) {
            sql += condition;
        }
        sql += " GROUP BY o.id ) aaa ";
        Object aa = BaseDB.queryMap(sql).get(0).get("aa");
        return Integer.parseInt(aa + "");
    }

    @Override
    public int addOrder(TOrder order) {
        String sql = "insert into t_order(createtime,uid,name,tel,roomtype,count,rzdate,leavedate,price) " +
                "values(now(),?,?,?,?,?,?,?,?)";
        int update = BaseDB.update(sql, order.getUid(), order.getName(),
                order.getTel(), order.getRoomtype(), order.getCount(),
                order.getRzdate(), order.getLeavedate(), order.getPrice());
        return update;
    }

    @Override
    public int changeOrderStatus(Integer status, Integer id) {
        String sql = "update t_order set status=? where id=?";
        return BaseDB.update(sql, status, id);
    }

    @Override
    public Map getInfoByOrderid(Integer orderid) {
        String sql = "select o.*, count(r.orderid) a \n" +
                "from t_order o left join t_ruzhudetail r \n" +
                "on o.id=r.orderid where o.id=" + orderid +
                " group by o.id";
        return BaseDB.queryMap(sql).get(0);
    }
}
